package com.android.demo_20220829.ui.activity;

import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.tool.SPUtil;
import com.android.baselibrary.view.Comm_EditView;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.android.demo_20220829.R;
import com.android.demo_20220829.adapter.HomeSearch_HistoryAdapter;
import com.android.demo_20220829.constants.Constants;
import com.android.demo_20220829.constants.SPConstants;
import com.android.demo_20220829.manager.API_Manager;
import com.blankj.utilcode.util.KeyboardUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.ll_head)
    LinearLayout ll_head;
    @BindView(R.id.et_word)
    Comm_EditView et_word;
    @BindView(R.id.ll_history)
    LinearLayout ll_history;
    @BindView(R.id.ll_content)
    LinearLayout ll_content;
    @BindView(R.id.recyclerview_history)
    RecyclerView recyclerview_history;
    @BindView(R.id.tv_no_history)
    TextView tv_no_history;
    @BindView(R.id.tv_clear_history)
    TextView tv_clear_history;

    List<Fragment> fragments = new ArrayList<>();
    List<String> titleList = new ArrayList<>();


    HomeSearch_HistoryAdapter historyAdapter;
    Handler handler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected boolean isImmersion() {
        return false;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).statusBarDarkFont(true).navigationBarDarkIcon(true).titleBar(ll_head).init();

        //单列
        recyclerview_history.setLayoutManager(new LinearLayoutManager(mContext));
        historyAdapter = new HomeSearch_HistoryAdapter(mContext, itemClick);
        recyclerview_history.setAdapter(historyAdapter);


        et_word.setInputCallBack(new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                String word = et_word.getText();
                if (TextUtils.isEmpty(word)) {
                    setWord();
                }
            }
        });

        et_word.et_input.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        et_word.et_input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN)) {
                    setWord();
                    return true;
                } else {
                    return false;
                }
            }
        });

        showHistory();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                KeyboardUtils.showSoftInput(et_word.et_input);
            }
        },1000);
    }

    CommCallBack itemClick = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            String word = (String) obj;
            et_word.setText(word);
            setWord();
        }
    };

    private void setWord() {
        String word = et_word.getText();
        if (TextUtils.isEmpty(word)) {
            ll_history.setVisibility(View.VISIBLE);
            ll_content.setVisibility(View.GONE);
            showHistory();
        } else {
            putHistory(word);
            ll_history.setVisibility(View.GONE);
            ll_content.setVisibility(View.VISIBLE);
        }
        //获取搜索数据

    }

    private void showHistory() {
        historyAdapter.setData(getHistory());

        recyclerview_history.setVisibility(View.GONE);
        tv_clear_history.setVisibility(View.GONE);
        tv_no_history.setVisibility(View.GONE);
        if (historyAdapter.getItemCount() == 0) {
            tv_no_history.setVisibility(View.VISIBLE);
        } else {
            recyclerview_history.setVisibility(View.VISIBLE);
            tv_clear_history.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {

    }

    @OnClick({R.id.tv_clear_history,R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_clear_history:
                clearHis("");
                break;
            case R.id.tv_cancel:
                finish();
                break;
        }
    }


    //插入历史记录
    private void putHistory(String text) {
        List<String> searchList = getHistory();
        if (searchList.contains(text)) return;
        searchList.add(0, text);
        if(searchList.size()>10)
        {
            searchList.remove(searchList.size()-1);
        }
        SPUtil.putValue(mContext, SPConstants.HOME_SEARCH_HISTORY, new Gson().toJson(searchList));
        showHistory();

    }

    //获取历史记录
    private List<String> getHistory() {
        String listStr = SPUtil.getStringValue(mContext, SPConstants.HOME_SEARCH_HISTORY, "");
        List<String> searchList = new Gson().fromJson(listStr, new TypeToken<List<String>>() {
        }.getType());
        if (searchList == null) searchList = new ArrayList<>();
        return searchList;
    }

    //清除历史记录
    private void clearHis(String text) {
        if (TextUtils.isEmpty(text)) {
            SPUtil.putValue(mContext, SPConstants.HOME_SEARCH_HISTORY, "");
        } else {
            List<String> searchList = getHistory();
            searchList.remove(text);
            SPUtil.putValue(mContext, SPConstants.HOME_SEARCH_HISTORY, new Gson().toJson(searchList));
        }
        showHistory();
    }
}
